/*
 *  tsp_data.c
 *  TSP
 *
 *  Created by Nicolas Jozefowiez on 09/01/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include "tsp_data.h"

int tsp_data_myrand(int m){
  return (int)(((double)rand()/((double)RAND_MAX+1))*m);
}

void tsp_data_read(TSP_DATA *d, char *name){
  FILE *f;
  int val, i, j;

  f = (FILE *)fopen(name, "r");

  fscanf(f, "%d", &val);
  d->nbn = val;

  d->d = (double **)malloc(d->nbn*sizeof(double *));
  for (i = 0; i < d->nbn; i++)
    d->d[i] = (double *)malloc(d->nbn*sizeof(double));

  for (i = 0; i < d->nbn; i++)
    for (j = 0; j < d->nbn; j++){
      fscanf(f, "%d", &val);
      d->d[i][j] = (double)val; /* = d->d[j][i] = (double)val; */
    }

  for (i = 0; i < d->nbn; i++)
    d->d[i][i] = 0.0;
}

/* void tsp_data_read(TSP_DATA *d, char *name){ */
/*   FILE *f; */
/*   int val, i, j; */

/*   f = (FILE *)fopen(name, "r"); */

/*   fscanf(f, "%d", &val); */
/*   d->nbn = 5;/\* val; *\/ */

/*   d->d = (double **)malloc(d->nbn*sizeof(double *)); */
/*   for (i = 0; i < d->nbn; i++) */
/*     d->d[i] = (double *)malloc(d->nbn*sizeof(double)); */

/*   for (j = 0; j < d->nbn; j++) */
/*     for (i = 0; i <= j; i++){ */
/*       fscanf(f, "%d", &val); */
/*       d->d[i][j] = d->d[j][i] = /\* (double)val *\/5.0; */
/*     } */

/*   for (i = 0; i < d->nbn; i++) */
/*     d->d[i][i] = 0.0; */
/* } */

void tsp_data_free(TSP_DATA *d){
  int i;

  for (i = 0; i < d->nbn; i++)
    free(d->d[i]);
  free(d->d);
}
